﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using InsuranceCompany.Helpers;
using InsuranceCompany.Core.Domain.Objects;
using InsuranceType = InsuranceCompany.Core.Domain.InsuranceArea.InsuranceType;

namespace InsuranceCompany.Categories
{
    public partial class PolicyCategories : InsuranceForm, InsuranceMainModuleInterface
    {
        private InsuranceType SelectedIType;
        private ObjectsCollection ITypesAll;
        public PolicyCategories()
        {
            InitializeComponent();
        }
        public PolicyCategories(ref List<ObjectsCollection> objects)
        {
            InitializeComponent();
            this.Objects = objects;
            this.ITypesAll = this.Objects.Find(x => x.GetName() == "PolicyTypes");
            this.UpdateItemsListView();

        }
        private void UpdateItemsListView()
        {
            this.PolicesList.Items.Clear();
            foreach (var item in this.ITypesAll.Items)
            {           
                ListViewItem listViewItem = new ListViewItem();
                InsuranceType itype = (InsuranceType)item;
                listViewItem.SubItems[0].Text = itype.GetId().ToString();               
                listViewItem.SubItems.Add(itype.Name);
                listViewItem.SubItems.Add(itype.RiskMaxSum.ToString());
                listViewItem.SubItems.Add(itype.MinCost.ToString());                  
                this.PolicesList.Items.Add(listViewItem);
            }
        }
        private void EnabledEditButtons(bool enable)
        {
            this.EditPolicy.Enabled = enable;
            this.DeletePolicy.Enabled = enable;
        }
        private void PolicyCategories_Load(object sender, EventArgs e)
        {

        }
        private void AddItemClick(object sender, EventArgs e)
        {
            List<ObjectsCollection> objects = this.Objects;
            Edit editForm = new Edit(ref objects);
            this.Hide();
            editForm.ShowDialog();
            this.Objects = objects;
            this.Show();
            this.UpdateItemsListView();
        }
        private void EditItemClick(object sender, EventArgs e)
        {
            List<ObjectsCollection> objects = this.Objects;
            Edit editForm = new Edit(ref objects, this.SelectedIType);
            this.Hide();
            editForm.ShowDialog();
            this.Objects = objects;
            this.Show();
            this.UpdateItemsListView();
        }
        private void DeleteItemClick(object sender, EventArgs e)
        {
            this.ITypesAll.Items.Remove(this.SelectedIType);
            this.UpdateItemsListView();
        }
        private void ItemsListViewItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            if (this.PolicesList.SelectedItems.Count == 1)
            {
                this.EnabledEditButtons(true);
                ListView.SelectedListViewItemCollection selectedItem = this.PolicesList.SelectedItems;
                int id = -1;
                foreach (ListViewItem item in selectedItem)
                {
                    id = Int32.Parse(item.SubItems[0].Text);
                    break;
                }
                this.SelectedIType = (InsuranceType)this.ITypesAll.Items.Find(x => x.GetId() == id);
            }
            else
                this.EnabledEditButtons(false);
        }
    }
}
